package com.scalar.chiptrack.jasper.engineering.actions;

import com.scalar.chiptrack.db.DBUtil;
import com.scalar.chiptrack.db.DataSource;
import com.scalar.chiptrack.jasper.base.AbstractJasperReportsAction;
import com.scalar.chiptrack.jasper.base.JasperReportsHelper;
import com.scalar.chiptrack.jasper.base.datasource.CTBeanCollectionDatasource;
import com.scalar.chiptrack.jasper.base.datasource.ChiptrackJasperDataSource;
import com.scalar.chiptrack.jasper.engineering.beans.BinYieldData;

import javax.servlet.http.HttpServletRequest;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * User: sujan
 * Date: Mar 27, 2011
 * Time: 2:30:44 PM
 *
 * Jasper report action class.
 */
public class YieldReportAction extends AbstractJasperReportsAction {

	public ChiptrackJasperDataSource getJasperDataSource(Map paramMap, HttpServletRequest request) throws Exception {
		String reportName = JasperReportsHelper.getReportName(request);
		Connection conn = null;
		CallableStatement cstmt = null;
		ResultSet rset = null;
		ChiptrackJasperDataSource chiptrackJasperDataSource = null;
		try {
				int paramNo = 1;
				conn = DataSource.getConnection();
				System.out.println("REport Name " + reportName);
				if( "engineering/yield_report".equalsIgnoreCase( reportName ) )
				{
					cstmt = conn.prepareCall("{Call SP_TEST_BIN_YIELD_REPORT(?, ?, ?, ?, ?, ?, ?, ?)}");
					cstmt.setString(paramNo++, (String) paramMap.get("prompt0"));
					cstmt.setString(paramNo++, (String) paramMap.get("prompt1"));
					cstmt.setString(paramNo++, (String) paramMap.get("prompt2"));
					cstmt.setString(paramNo++, (String) paramMap.get("prompt3"));
					cstmt.setString(paramNo++, (String) paramMap.get("prompt4"));
					cstmt.setString(paramNo++, (String) paramMap.get("prompt5"));
					cstmt.setString(paramNo++, "XLREPORT");
					cstmt.setString(paramNo++, (String) paramMap.get("prompt7"));
				}
				else
				{
					cstmt = conn.prepareCall("{Call SP_SORT_ALL_BIN_HISTORY_REPORT(?, ?, ?, ?, ?, ?, ?, ?)}");
					cstmt.setString(paramNo++, (String) paramMap.get("prompt0"));
					cstmt.setString(paramNo++, (String) paramMap.get("prompt1"));
					cstmt.setString(paramNo++, (String) paramMap.get("prompt2"));
					cstmt.setString(paramNo++, (String) paramMap.get("prompt3"));
					cstmt.setString(paramNo++, (String) paramMap.get("prompt4"));
					cstmt.setString(paramNo++, (String) paramMap.get("prompt5"));
					cstmt.setString(paramNo++, "XLREPORT");
					cstmt.setString(paramNo++, (String) paramMap.get("prompt7"));
				}
				rset = cstmt.executeQuery();
				chiptrackJasperDataSource = prepareBinYieldBeanDataSource(rset);
			} finally {
				DBUtil.close(conn, rset, cstmt);
			}
		return chiptrackJasperDataSource;
	}

	@Override
	public void setReportParams(Map paramMap, HttpServletRequest request) {
		super.setReportParams(paramMap, request);
	}

	private ChiptrackJasperDataSource prepareBinYieldBeanDataSource(ResultSet rset) throws SQLException {
		List binYieldList = new ArrayList();
		BinYieldData bin2 =  new BinYieldData("Bin 2",0);
		BinYieldData bin3 =  new BinYieldData("Bin 3",0);
		BinYieldData bin4 =  new BinYieldData("Bin 4",0);
		BinYieldData bin5 =  new BinYieldData("Bin 5",0);
		BinYieldData bin6 =  new BinYieldData("Bin 6",0);
		BinYieldData bin7 =  new BinYieldData("Bin 7",0);
		BinYieldData bin8 =  new BinYieldData("Bin 8",0);
		BinYieldData bin9 =  new BinYieldData("Bin 9",0);
		BinYieldData bin10 = new BinYieldData("Bin 10",0);
		BinYieldData bin11 = new BinYieldData("Bin 11",0);
		BinYieldData bin12 = new BinYieldData("Bin 12",0);
		BinYieldData bin13 = new BinYieldData("Bin 13",0);
		BinYieldData bin14 = new BinYieldData("Bin 14",0);
		BinYieldData bin15 = new BinYieldData("Bin 15",0);
		BinYieldData bin16 = new BinYieldData("Bin 16",0);
		BinYieldData bin17 = new BinYieldData("Bin 17",0);
		BinYieldData bin18 = new BinYieldData("Bin 18",0);
		BinYieldData bin19 = new BinYieldData("Bin 19",0);
		BinYieldData bin20 = new BinYieldData("Bin 20",0);
		BinYieldData bin21 = new BinYieldData("Bin 21",0);
		BinYieldData bin22 = new BinYieldData("Bin 22",0);
		BinYieldData bin23 = new BinYieldData("Bin 23",0);
		BinYieldData bin24 = new BinYieldData("Bin 24",0);
		BinYieldData bin25 = new BinYieldData("Bin 25",0);
		BinYieldData bin26 = new BinYieldData("Bin 26",0);
		BinYieldData bin27 = new BinYieldData("Bin 27",0);
		BinYieldData bin28 = new BinYieldData("Bin 28",0);
		BinYieldData bin29 = new BinYieldData("Bin 29",0);
		BinYieldData bin30 = new BinYieldData("Bin 30",0);
		BinYieldData bin31 = new BinYieldData("Bin 31",0);
		BinYieldData bin32 = new BinYieldData("Bin 32",0);
		BinYieldData bin33 = new BinYieldData("Bin 33",0);
		BinYieldData bin34 = new BinYieldData("Bin 34",0);
		BinYieldData bin35 = new BinYieldData("Bin 35",0);
		BinYieldData bin36 = new BinYieldData("Bin 36",0);
		BinYieldData bin37 = new BinYieldData("Bin 37",0);
		BinYieldData bin38 = new BinYieldData("Bin 38",0);
		BinYieldData bin39 = new BinYieldData("Bin 39",0);
		BinYieldData bin40 = new BinYieldData("Bin 40",0);
		if (rset != null) {
			while (rset.next()) {
				bin2.setQty(bin2.getQty()+rset.getInt("Bin 2"));
				bin3.setQty(bin3.getQty()+rset.getInt("Bin 3"));
				bin4.setQty(bin4.getQty()+rset.getInt("Bin 4"));
				bin5.setQty(bin5.getQty()+rset.getInt("Bin 5"));
				bin6.setQty(bin6.getQty()+rset.getInt("Bin 6"));
				bin7.setQty(bin7.getQty()+rset.getInt("Bin 7"));
				bin8.setQty(bin8.getQty()+rset.getInt("Bin 8"));
				bin9.setQty(bin9.getQty()+rset.getInt("Bin 9"));
				bin10.setQty(bin10.getQty()+rset.getInt("Bin 10"));
				bin11.setQty(bin11.getQty()+rset.getInt("Bin 11"));
				bin12.setQty(bin12.getQty()+rset.getInt("Bin 12"));
				bin13.setQty(bin13.getQty()+rset.getInt("Bin 13"));
				bin14.setQty(bin14.getQty()+rset.getInt("Bin 14"));
				bin15.setQty(bin15.getQty()+rset.getInt("Bin 15"));
				bin16.setQty(bin16.getQty()+rset.getInt("Bin 16"));
				bin17.setQty(bin17.getQty()+rset.getInt("Bin 17"));
				bin18.setQty(bin18.getQty()+rset.getInt("Bin 18"));
				bin19.setQty(bin19.getQty()+rset.getInt("Bin 19"));
				bin20.setQty(bin20.getQty()+rset.getInt("Bin 20"));
				bin21.setQty(bin21.getQty()+rset.getInt("Bin 21"));
				bin22.setQty(bin22.getQty()+rset.getInt("Bin 22"));
				bin23.setQty(bin23.getQty()+rset.getInt("Bin 23"));
				bin24.setQty(bin24.getQty()+rset.getInt("Bin 24"));
				bin25.setQty(bin25.getQty()+rset.getInt("Bin 25"));
				bin26.setQty(bin26.getQty()+rset.getInt("Bin 26"));
				bin27.setQty(bin27.getQty()+rset.getInt("Bin 27"));
				bin28.setQty(bin28.getQty()+rset.getInt("Bin 28"));
				bin29.setQty(bin29.getQty()+rset.getInt("Bin 29"));
				bin30.setQty(bin30.getQty()+rset.getInt("Bin 30"));
				bin31.setQty(bin31.getQty()+rset.getInt("Bin 31"));
				bin32.setQty(bin32.getQty()+rset.getInt("Bin 32"));
				bin33.setQty(bin33.getQty()+rset.getInt("Bin 33"));
				bin34.setQty(bin34.getQty()+rset.getInt("Bin 34"));
				bin35.setQty(bin35.getQty()+rset.getInt("Bin 35"));
				bin36.setQty(bin36.getQty()+rset.getInt("Bin 36"));
				bin37.setQty(bin37.getQty()+rset.getInt("Bin 37"));
				bin38.setQty(bin38.getQty()+rset.getInt("Bin 38"));
				bin39.setQty(bin39.getQty()+rset.getInt("Bin 39"));
				bin40.setQty(bin40.getQty()+rset.getInt("Bin 40"));
			}
		}
		binYieldList.add(bin2);
		binYieldList.add(bin3);
		binYieldList.add(bin4);
		binYieldList.add(bin5);
		binYieldList.add(bin6);
		binYieldList.add(bin7);
		binYieldList.add(bin8);
		binYieldList.add(bin9);
		binYieldList.add(bin10);
		binYieldList.add(bin11);
		binYieldList.add(bin12);
		binYieldList.add(bin13);
		binYieldList.add(bin14);
		binYieldList.add(bin15);
		binYieldList.add(bin16);
		binYieldList.add(bin17);
		binYieldList.add(bin18);
		binYieldList.add(bin19);
		binYieldList.add(bin20);
		binYieldList.add(bin21);
		binYieldList.add(bin22);
		binYieldList.add(bin23);
		binYieldList.add(bin24);
		binYieldList.add(bin25);
		binYieldList.add(bin26);
		binYieldList.add(bin27);
		binYieldList.add(bin28);
		binYieldList.add(bin29);
		binYieldList.add(bin30);
		binYieldList.add(bin31);
		binYieldList.add(bin32);
		binYieldList.add(bin33);
		binYieldList.add(bin34);
		binYieldList.add(bin35);
		binYieldList.add(bin36);
		binYieldList.add(bin37);
		binYieldList.add(bin38);
		binYieldList.add(bin39);
		binYieldList.add(bin40);

		return new CTBeanCollectionDatasource(binYieldList);
	}
}
